import { listAbnormalCount } from '@/api/workorder';
import store from '@/store';

const order = {
  state: {
    totalNum: 0,
    abnormalNum: 0,
    refundCodeNum: 0,
    updateGoodsNum: 0,
    updateGoodNotPassNum: 0,
  },
  actions: {
    getNum({ state, commit }, customParams = {}) {
      const permission = store.getters.permission
      const params = {
        all: permission['inquiry_abnormal_notice_all'] ? 1 : 0,
        ...customParams,
      }
      return listAbnormalCount(params).then(res => {
        const data = {
          abnormalNum: permission['inquiry_abnormal_notice_workorder'] ? res.data.data['1'] : 0,
          refundCodeNum: permission['inquiry_abnormal_notice_qrcode'] ? res.data.data['2'] : 0,
          updateGoodsNum: permission['inquiry_abnormal_notice_updategoods'] ? res.data.data['3'] : 0,
          updateGoodNotPassNum: res.data.data['4'] || 0
        }
        commit('setPendingNum', data)
        return data
      })
    }
  },
  mutations: {
    setPendingNum(state, payload) {
      state.abnormalNum = payload.abnormalNum
      state.refundCodeNum = payload.refundCodeNum
      state.updateGoodsNum = payload.updateGoodsNum
      state.updateGoodNotPassNum = payload.updateGoodNotPassNum
      state.totalNum = payload.abnormalNum + payload.refundCodeNum + payload.updateGoodsNum + payload.updateGoodNotPassNum
    }
  }
};

export default order;